import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { ApiService } from 'src/app/core/api';
import { MessageService } from 'src/app/core/service/message.service';
import { FormBuilder, Validators } from '@angular/forms';
import { DatePipe } from '@angular/common';
import { I18nService } from 'src/app/core/i18n/i18n.service';

@Component({
    templateUrl: './index.html',
    styles: [
        `
          .tab-head .ant-btn{
            margin-right: 12px;
          }
          .tab-head .ant-input-search-button{
              margin:0;
          }
          
        `
    ],

})
export class CPComponent implements OnInit {
    page = {
        pageNo: 1,
        pages: 10,
        title: '',
        displayPriority: '',
        beginTime: '',
		endTime: '',
		status:'Y',
    };
    dateRange;
    dataList = [];
    isVisible = false;
    isOkLoading = false;
    tableLoading = false;
    modalTitle;
    selectedRowData: any = {};
    validateForm;
    cpStatus = {};
    providerType = {};
    constructor(
        private apiService: ApiService,
        private nzMessageService: NzMessageService,
        private message: MessageService,
        private fb: FormBuilder,
        private date: DatePipe,
        private i18n:I18nService
    ) {
        this.i18n.get(['cpStatus','providerType','pagetitle']).subscribe((res:any)=>{
            this.cpStatus = res.cpStatus;
            this.providerType = res.providerType;
            const pagetitle = res.pagetitle;
            this.message.setBreadcrumb({
                breadcrumbs: [{ name: pagetitle.title3}],
            });
        })
    }
    ngOnInit() {
        this.modalTitle = '';
        this.validateForm = this.fb.group({
            providerType: ['', [Validators.required]],
            title: [''],
            id: [''],
            serviceProvider: [''],
            url: [''],
            displayPriority: [''],
            image: ['', [Validators.required]],
            status: ['', [Validators.required]]
        });
        this.loadList();
    }
    loadList() {
        this.tableLoading = true;
        this.apiService.post('旅游产品列表', { ...this.page }).then((res: any) => {
            this.dataList = res.data.list;

            this.page.pageNo = res.data.pageNo;
            this.page.pages = res.data.pages * 10;
            this.tableLoading = false;
        });
    };
    changeCurrentPage(e) {
        if (e) {
            this.page.pageNo = e;
            this.loadList();
        }
    }
    selRow;
    isDel:boolean;
    updateData(data,bool) {
        bool ? this.modalTitle = '旅游产品详情' :  this.modalTitle = '编辑旅游产品';
        this.isDel = bool;
        this.validateForm.reset();
        this.apiService.post('产品详情', { travelProductId: data.id }).then((res: any) => {
            this.selRow = res.data;
            this.isVisible = true;
            this.validateForm.patchValue(res.data);
            this.validateForm.patchValue({ url: data.serviceProvider });
        });
    };
    handleCancel() {
        this.isVisible = false;
    };
    handleOk() {
        this.isOkLoading = false;
        this.isOkLoading = true;
        const data = { ...this.validateForm.getRawValue() };
        this.selRow.image == data.image ? data.image = '' : data.image;
        if(!this.isDel){
            this.apiService.post('旅游产品编辑', data).then((res: any) => {
                this.isOkLoading = false;
                this.isVisible = false;
                this.loadList();
            });
        }else{
            this.isVisible = false;
            this.isOkLoading = false;
        }
    };
    onChangeStatus(data, s) {
        this.apiService.post('激活产品', { travelProductId: data.id, status: s }).then((res: any) => {
            this.loadList();
        });
    };
    onChangeDate(e) {
        this.page['beginTime'] = e[0] ? this.date.transform(e[0], 'yyyy-MM-dd') : '';
        this.page['endTime'] = e[1] ? this.date.transform(e[1], 'yyyy-MM-dd') : '';
    };
    sherch() {
        this.loadList();
    };
}
